package cn.zj.logistics.controller;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import cn.zj.logistics.mo.MessageObject;
import cn.zj.logistics.pojo.Permission;
import cn.zj.logistics.pojo.PermissionExample;
import cn.zj.logistics.pojo.PermissionExample.Criteria;
import cn.zj.logistics.pojo.Role;
import cn.zj.logistics.pojo.RoleExample;
import cn.zj.logistics.service.PermissionService;
import cn.zj.logistics.service.RoleService;

@Controller
@RequestMapping("/permission")
public class PermissionController {
	
	@Autowired
	private PermissionService permissionService;
	
	
	//页面跳转功能
	@RequestMapping("/permissionPage")
	@RequiresPermissions("permission:permissionPage")
	public String permissionPage() {
		
		return "permissionPage";
	}
	
	@RequestMapping("/permissionEdit")
	public String permissionEdit(Model m,Long permissionId) {
		
		//如果用户id不等于空，说明是修改操作，查询用户对象共享过去，以供回显
		if(permissionId !=null) {
			Permission permission = permissionService.selectByPrimaryKey(permissionId);
			m.addAttribute("permission", permission);
		}
		
		//查询父权限：只有菜单权限才能作为父权限
		
		PermissionExample example = new PermissionExample();
		//只有菜单权限才能作为父权限
		Criteria criteria = example.createCriteria();
		criteria.andTypeEqualTo("menu");
		
		List<Permission> parents = permissionService.selectByExample(example );
		m.addAttribute("parents", parents);
		
		return "permissionEdit";
	}
	
	
	//返回所有的权限数据（以供角色分配权限时候使用）
	
	@ResponseBody
	@RequestMapping("/getAllPermission")
	public List<Permission> getAllPermission(){
		PermissionExample example = new PermissionExample();
		List<Permission> permissions = permissionService.selectByExample(example);
		return permissions;
	}
	
	
	@RequestMapping("/checkPermissionName")
	@ResponseBody
	public boolean checkPermissionName(String permissionName) {
		
		PermissionExample example = new PermissionExample();
		Criteria criteria = example.createCriteria();
		criteria.andNameEqualTo(permissionName);
		
		List<Permission> permissions = permissionService.selectByExample(example );
		
		return permissions.size() == 0 ? true:false;
	}
	
	
	//新增功能
	@RequestMapping("/insert")
	@ResponseBody
	@RequiresPermissions("permission:insert")
	public MessageObject insert(Permission permission) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "新增数据失败，请联系管理员");
		
		int row = permissionService.insert(permission);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "新增数据成功");
		}
		
		return mo;
	}
	//修改功能
	@RequestMapping("/update")
	@ResponseBody
	@RequiresPermissions("permission:update")
	public MessageObject update(Permission permission) {
		
		
		MessageObject mo = MessageObject.createMessageObject(0, "修改数据失败，请联系管理员");
		
		int row = permissionService.updateByPrimaryKeySelective(permission);
		if(row == 1) {
			mo = MessageObject.createMessageObject(1, "修改数据成功");
		}
		
		return mo;
	}
	
	
	//数据列表功能
	@ResponseBody
	@RequestMapping("/list")
	@RequiresPermissions("permission:list")
	public PageInfo<Permission> list(@RequestParam(defaultValue = "1") int pageNum,
				@RequestParam(defaultValue = "10") int pageSize,String keyword){
		
		
		PageHelper.startPage(pageNum, pageSize);
		
		PermissionExample example = new PermissionExample();
		
		//条件搜索查询
		
		if(StringUtils.isNotBlank(keyword)) {
			//根据权限名称模糊查询
			
			Criteria criteria1 = example.createCriteria();
			criteria1.andNameLike("%"+keyword+"%");
			
			
		}
		List<Permission> permissions = permissionService.selectByExample(example);

		
		PageInfo<Permission> pageInfo = new PageInfo<Permission>(permissions);
		
		return pageInfo;
	}
	
	@RequestMapping("/delete")
	@RequiresPermissions("permission:delete")
	@ResponseBody
	public MessageObject delete(Long permissionId) {
		
		
		
		//删除之前先判断当前权限是否还拥有子权限，如果有，不能删除
		
		PermissionExample example = new PermissionExample();
		Criteria createCriteria = example.createCriteria();
		createCriteria.andParentIdEqualTo(permissionId);
		
		List<Permission> children = permissionService.selectByExample(example);
		
		
		//创建消息对象
		MessageObject mo = MessageObject.createMessageObject(0, "删除数据失败，请联系管理员");
		
		if(children.size() > 0) {
			mo = MessageObject.createMessageObject(0, "此权限还有子权限，不能删除");
		}else {
			int row = permissionService.deleteByPrimaryKey(permissionId);
			
			if(row == 1) {
				mo = MessageObject.createMessageObject(1, "删除数据成功");
			}
			
		}
		
		
		
		return mo;
	}
	
	
}
